package com.zzj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.base.Result;
import com.zzj.entity.Like;
import com.zzj.model.api.LikeApi;
import com.zzj.model.dto.QueryListDTO;
import com.zzj.model.entity.LikeEntity;
import com.zzj.service.ILikeService;
import com.zzj.service.RedisService;
import com.zzj.utils.JWTUtil;
import com.zzj.utils.QueryUtil;
import com.zzj.utils.RedisUtil;
import com.zzj.utils.Utils;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-03-21
 */
@RestController
@RequestMapping("/like")
public class LikeController implements LikeApi {
    @Resource
    private ILikeService likeService;

    @Resource
    private RedisService redisService;
    /**
     * 获取未读赞数量
     *
     * @param token
     * @return
     */
    @PostMapping("getLikeNoReadNum")
    public Result getLikeNoReadNum(@RequestHeader(value = "Authorization") String token) {
        String username = JWTUtil.getUsername(token);
        String likeNoReadId = redisService.getLikeNoReadId(username);
        if(likeNoReadId!=null&&likeNoReadId!=""){
            return Result.success(String.valueOf(likeNoReadId).split(",").length);
        }
        return Result.success(0);
    }

    @Transactional
    @PostMapping("likeQueryList")
    public Result  likeQueryList(@RequestHeader(value = "Authorization") String token,
                                 @RequestBody @Validated QueryListDTO in) {

        QueryWrapper<Like> queryWrapper = QueryUtil.paramsWrapper(in.getAndParams(), in.getOrParams(), in.getOrderParams());
        Page<Like> likePage = new Page<>(in.getPageNo(), in.getPageSize());
        IPage<Like> likeIPage = likeService.getBaseMapper().selectPage(likePage, queryWrapper);
        List<Like> likes = likeIPage.getRecords();
        String username = JWTUtil.getUsername(token);
        String likeNoReadId = redisService.getLikeNoReadId(username);
        if (likeNoReadId != null && !likeNoReadId.equals("")) {
            List<String> noReads = Utils.stringToList(likeNoReadId, ",");
            List<Like> list = likes.stream()
                    .map(like -> noReads.stream()
                            .filter(str -> like.getId() == Integer.parseInt(str))
                            .findFirst()
                            .map(str -> {
                                like.setIsRead(0);
                                return like;
                            }).orElse(like)).toList();
        }
        likeIPage.setRecords(likes);
        redisService.removeLikeNoReadId(username);
        return Result.success(likeIPage);
    }



    @Override
    public LikeEntity remove(int targetId, String fromUsername) {
        Like one = likeService.findLike(targetId, fromUsername);
        LikeEntity likeEntity = JSONObject.parseObject(JSON.toJSONString(one), LikeEntity.class);
        boolean remove = likeService.remove(targetId, fromUsername);
        if(remove){
            return likeEntity;
        }
        return null;
    }

    @Override
    public LikeEntity save(int targetId, int targetType ,String fromUsername, String toUsername) {
        Like save = likeService.save(targetId,targetType, fromUsername, toUsername);
        if(save!=null){
            return  JSONObject.parseObject(JSON.toJSONString(save), LikeEntity.class);
        }
        return null;
    }
}
